package xin.fsqr520.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.fsqr520.project.domain.po.WsChatMessage;
import xin.fsqr520.project.domain.vo.Result;
import xin.fsqr520.project.mapper.WschatMessageMapper;

import java.util.List;

/**
 * ws聊天相关的HTTP请求
 */
@RestController
@RequestMapping("/wschat")
public class WsChatController {
    @Autowired
    private WschatMessageMapper wschatMessageMapper;

    /**
     * 查询两个user之间的聊天记录
     *
     * @param user1
     * @param user2
     * @return
     */
    @GetMapping("/historymessage")
    public Result<List<WsChatMessage>> history(String user1, String user2) {
        LambdaQueryWrapper<WsChatMessage> wrapper = new LambdaQueryWrapper<WsChatMessage>()
                // 1. 先加一个and条件块，这个块里两个条件：sender=user1 且 receiver=user2
                .and(w -> w.eq(WsChatMessage::getSender, user1)
                        .eq(WsChatMessage::getReceiver, user2))

                // 2. 或者满足下面的条件块：sender=user2 且 receiver=user1
                .or(w -> w.eq(WsChatMessage::getSender, user2)
                        .eq(WsChatMessage::getReceiver, user1))

                // 3. 最后按 sendTime 就是这样吧
                .orderByAsc(WsChatMessage::getSendTime);

        return Result.success(wschatMessageMapper.selectList(wrapper));
    }
}
